package com.scancode.ocr;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scancode.ocr.osbar.OsBarUtil;
import com.shouzhong.scanner.Callback;
import com.shouzhong.scanner.Result;
import com.shouzhong.scanner.ScannerView;

public class OcrIDActivity extends AppCompatActivity implements View.OnClickListener {

    private ScannerView scannerView;
    private View viewBackCar;
    private LinearLayout lineCarRight, lineCarLeft;
    private TextView tvBackCar, tvTitleCar, tvRightCar;
    private RelativeLayout barTopCar;

    public static OcrLiveEvent<OcrResultBean> ocrResult = new OcrLiveEvent<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ocr_idactivity);

        barTopCar = findViewById(R.id.rey_ocrscan_top);
        scannerView = findViewById(R.id.scannerView);
        viewBackCar = findViewById(R.id.view_ocrscan_back);
        tvBackCar = findViewById(R.id.tv_ocrscan_back);
        tvTitleCar = findViewById(R.id.tv_ocrscan_title);
        tvRightCar = findViewById(R.id.tv_ocrscan_right);
        lineCarRight = findViewById(R.id.line_ocrscan_right);
        lineCarLeft = findViewById(R.id.line_ocrscan_left);

        init();
        initScan();
    }

    @SuppressLint("LongLogTag")
    private void init() {
        lineCarLeft.setOnClickListener(this);
        barTopCar.setBackgroundColor(OcrIDClient.getClient().getBarBgColor());
        if (OcrIDClient.getClient().isToOsBar()) {
            OsBarUtil.setStatusBarColor(this, OcrIDClient.getClient().getBarTextColor(), OcrIDClient.getClient().getBarBgColor());
        }
        viewBackCar.setBackgroundResource(OcrIDClient.getClient().getImgBack());
        //返回键
        if (OcrIDClient.getClient().isShowBackText()) {
            tvBackCar.setText(OcrIDClient.getClient().getBarBackText());
            tvBackCar.setVisibility(View.VISIBLE);
            tvBackCar.setTextColor(OcrIDClient.getClient().getBarTextColor());
        } else {
            tvBackCar.setVisibility(View.INVISIBLE);
        }
        //标题
        tvTitleCar.setText(OcrIDClient.getClient().getBarTitleText());
        tvTitleCar.setTextColor(OcrIDClient.getClient().getBarTextColor());
        //右部相册
        if (OcrIDClient.getClient().isOpenLight()) {
            lineCarRight.setVisibility(View.VISIBLE);
            tvRightCar.setText("补光");
            tvRightCar.setTextColor(OcrIDClient.getClient().getBarTextColor());
            lineCarRight.setOnClickListener(this);
        } else {
            lineCarRight.setVisibility(View.INVISIBLE);
        }

    }

    /**
     * 初始化ORC 扫码识别
     */
    private void initScan() {
        Log.e("initScan:", "初始化");
        scannerView.setViewFinder(new ViewFinder(this));//设置扫描区域
        scannerView.setShouldAdjustFocusArea(true);
        scannerView.setSaveBmp(false);
        scannerView.setRotateDegree90Recognition(true);
        // 启用身份证识别（第二种方式）
        scannerView.setEnableIdCard2(true);
        //scannerView.setEnableLicensePlate(true);
        scannerView.setCallback(new Callback() {
            @Override
            public void result(Result result) {
                if(result==null){
                    Log.e("识别结果：", "识别失败:");
                }else{
                    Log.e("识别结果：", "识别成功:"+result.toString());
                    ocrResult.setValue(new OcrResultBean(OcrIDClient.getClient().getZxingTag(), result.data));
                    finish();
                }
                //scannerView.restartPreviewAfterDelay(2000);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        scannerView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        scannerView.onPause();
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.line_ocrscan_right) {
            if (scannerView.isFlashOn()) {
                scannerView.setFlash(false);
            } else {
                scannerView.setFlash(true);
            }
        } else if (id == R.id.line_ocrscan_left) {
            finish();
        }
    }
}